package operation.chef;

import zy.Warehouse;

import java.util.Map;
import java.util.Scanner;

/*
 *@Student 张策
 */
public class chef3 implements Ioperation {
    Scanner scanner=new Scanner(System.in);
    @Override
    public void work(Warehouse warehouse) {
        System.out.println("------------------订单列表------------------");
        warehouse.printOrder();
        System.out.println("------------------订单列表------------------");
        System.out.println("共计"+warehouse.orders.size()+"条数据");
        if(Warehouse.orders.isEmpty()){
            System.out.println("输入N退出:");
            String or=scanner.nextLine();
            while (!or.equals("N")){
                System.out.println("请重新输入");
                or=scanner.nextLine();
            }
            return;
        }
        System.out.println("请输入你要处理的订单编号:");
        String codeName=scanner.nextLine();
        while (!warehouse.xOrder(codeName)){
            System.out.println("订单号错误请再次输入");
            codeName=scanner.nextLine();
        }
        System.out.println("是否继续(Y/N):");
        String or=scanner.nextLine();
        while (!or.equals("Y")&&!or.equals("N")){
            System.out.println("请重新输入");
            or=scanner.nextLine();
        }
        if(or.equals("Y")){
            Map<String, Integer> map = warehouse.findOrder(codeName).food;
            if(isInventorySufficient(warehouse,codeName)){
                for (String key : map.keySet()) {
                    int num=map.get(key);
                    warehouse.nameDish(key).setStock(warehouse.nameDish(key).getStock()-num);
                }
                warehouse.orders.remove(warehouse.findOrder(codeName));
                System.out.println("处理成功");
            }else {
            for (String key : map.keySet()) {
                int num=map.get(key);
                int stock=warehouse.dishes.get(warehouse.xDish(key)).getStock();
                if(stock==0){
                    System.out.println(key+"库存为零，主厨需要炒菜");
                }
                if(num>stock) {
                    System.out.println("主厨还需要炒" + key + (num - stock) + "份");
                }
            }
        }
        }else if(or.equals("N")){
        }else {
            System.out.println("输入错误请重新输入");
        }
    }
    //判断库存是否满足订单
    public boolean isInventorySufficient(Warehouse warehouse,String codeName){
        Map<String, Integer> map = warehouse.findOrder(codeName).food;
        for (String key : map.keySet()) {
            //订单要求的量
            int num=map.get(key);
            //库存量
            int stock=warehouse.dishes.get(warehouse.xDish(key)).getStock();
            if(num>stock){
                return false;
            }
        }
        return true;
    }
}
